package com.aplana.ccsd.service;

import info.magnolia.jcr.util.PropertyUtil;
import info.magnolia.module.blossom.dialog.TabBuilder;
import info.magnolia.module.templatingkit.dam.Asset;

import java.util.ArrayList;
import java.util.List;

import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aplana.ccsd.constants.WebConstants;
import com.aplana.ccsd.model.HtmlHorizontalBlockSize;
import com.aplana.ccsd.model.MenuItem;
import com.aplana.ccsd.model.Vendor;
import com.aplana.ccsd.model.VendorMenuItem;
import com.aplana.ccsd.util.DialogUtils;

@Service
public class VendorServiceImpl implements VendorService {
	
	@Autowired
	private TemplatingService templatingService;
	@Autowired
	private NavigationMenuService navigationMenuService;

	@Override
	public Vendor getVendor(Node node) {
		String text =  templatingService.getNodePropertyAsHtml(node, "vendorText");
    	Asset image =  templatingService.getNodePropertyAsImage(node, "vendorImage");
    	Asset menuImage =  templatingService.getNodePropertyAsImage(node, "menuVendorImage");
    	return new Vendor(text, image, menuImage);
	}

	@Override
	public void addVendorDialogControls(TabBuilder tabBuilder) {
		tabBuilder.addFckEditor("vendorText", "Текст", "Описание вендора");
		DialogUtils.addDamControl(tabBuilder, "vendorImage", "Логотип", "Логотип вендора");
		DialogUtils.addDamControl(tabBuilder, "menuVendorImage", "Логотип для меню", "Логотип вендора отображаемый в меню");
	}
	
	@Override
	public void addVendorsMenuDialogControls(TabBuilder tabBuilder) {
		tabBuilder.addEdit("title", "Title", "");
		tabBuilder.addLink("vendorsLink", "Страница c вендорами", "Ссылка на страницу с вендорами");
		tabBuilder.addEdit("columnNumber", "Число колонок", "Число колонок по горизонтали");
		tabBuilder.addEdit("containerWidth", "Ширина контейнера", "Ширина контейнера в px");
	}

	@Override
	public List<VendorMenuItem> getVendorsMenu(Node node) throws PathNotFoundException, RepositoryException {
		String vendorsNodeLink = PropertyUtil.getString(node, "vendorsLink");
		String columnNumber = PropertyUtil.getString(node, "columnNumber");
		String containerWidth = PropertyUtil.getString(node, "containerWidth");
		
		List<MenuItem> menuItems = navigationMenuService.getNavigationMenus(vendorsNodeLink, node);
		List<VendorMenuItem> vendorMenuItems = new ArrayList<VendorMenuItem>();
		
		int colNum = 0;
		if (org.apache.commons.lang.StringUtils.isBlank(columnNumber)) {
			if (menuItems.size()>6) {
				colNum = 6;
			} else {
				colNum = menuItems.size();
			}
		} else {
			colNum = Integer.decode(columnNumber);
		}
		
		int conWidth = WebConstants.SITE_WIDTH; // по умолчанию
		if (!org.apache.commons.lang.StringUtils.isBlank(containerWidth)) {
			conWidth = Integer.decode(containerWidth);
		}
		
		HtmlHorizontalBlockSize blockSize = new HtmlHorizontalBlockSize(colNum, 0, conWidth);
		int i = 0;
		for (MenuItem menu: menuItems) {
			blockSize.calculate(i);
			VendorMenuItem vendorMenu = new VendorMenuItem(getVendor(menu.getMenuPage()), menu, blockSize.getStyle());
			vendorMenuItems.add(vendorMenu);
			i++;
		}
		return vendorMenuItems;
	}

}